//
//  ManageJobIntensionVC.m
//  MyProject
//
//  Created by 石小强 on 2018/3/20.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "ManageJobIntensionVC.h"
#import "ManageJobIntensionCell.h"
#import "AddJobIntensionVC.h"
#import "CurrentStatusPopView.h"
#import "IntentionalModel.h"
#import "CandidateCompanyModel.h"
#import "SearchCompanyViewController.h"

@interface ManageJobIntensionVC ()

@property (nonatomic, strong) NSArray *status;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) CandidateCompanyModel *candidate;

@property (nonatomic, strong) CompanyModel *selectCompany;

@end

@implementation ManageJobIntensionVC {
    NSInteger _stateIndex;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"管理求职意向";
    self.status = @[@"离职(希望马上入职)", @"离职(一个月内入职)", @"在职(可以马上离职)", @"在职(考虑换环境中)", @"应届毕业生", @"在校生"];
    
    _stateIndex = 0;
    [self initSubViews];
    
    /** 查询意向职位列表 */
    [self loadIntentionPositionData];
    
    /** 获取求职者目标公司及状态 */
    [self loadTargetCompany];
}

/** 查询意向职位列表 */
- (void)loadIntentionPositionData {
    WS(weakSelf)
    [weakSelf showProgressWithView:self.view animated:YES];
    [SendRequest getIntentionalPositionWithResult:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:self.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"查询意向职位列表 = %@", [result objectForKey:@"data"]);
            
            NSArray *datas = [IntentionalModel mj_objectArrayWithKeyValuesArray:[result objectForKey:@"data"]];
            
            [weakSelf.dataArray removeAllObjects];
            [weakSelf.dataArray addObjectsFromArray:datas];
            
            [weakSelf.tableView reloadData];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}


/** 获取求职者目标公司及状态 */
- (void)loadTargetCompany {
    WS(weakSelf)
    [weakSelf showProgressWithView:self.view animated:YES];
    [SendRequest getCandidateTargetCompanyWithResult:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:self.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"获取求职者目标公司及状态 = %@", [result objectForKey:@"data"]);
            weakSelf.candidate = [CandidateCompanyModel mj_objectWithKeyValues:[result objectForKey:@"data"]];
            [weakSelf setupCandidateView];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

/** 更新目标公司及当前状态 */
- (void)updateTargetCompany {
    WS(weakSelf)
    [weakSelf showProgressWithView:self.view animated:YES];
    [SendRequest updateCandidateTargetCompanyWithCompany:self.selectCompany.companyId andState:[NSString stringWithFormat:@"%zd", _stateIndex] result:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:self.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"更新目标公司及当前状态成功");
            weakSelf.candidate.targetCompany = weakSelf.selectCompany.companyId;
            weakSelf.candidate.companyName = weakSelf.selectCompany.companyAbbreviatedName;
            weakSelf.candidate.candidateState = [NSString stringWithFormat:@"%zd", _stateIndex];
            
            [weakSelf setupCandidateView];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

- (void)setupCandidateView {
    self.targetCompanyL.text = self.candidate.companyName;
    _stateIndex = [self.candidate.candidateState integerValue];
    self.currentStatusL.text = [self.status objectAtIndex:_stateIndex];
}

- (void)initSubViews {
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ManageJobIntensionCell class]) bundle:nil] forCellReuseIdentifier:@"ManageJobIntensionCell"];
    self.tableBottomView.mj_size = CGSizeMake(kScreenWidth, 130);
    self.tableView.tableFooterView = self.tableBottomView;
}

- (IBAction)targetCompanyBtnClick:(id)sender {
    SearchCompanyViewController *scVC = [[SearchCompanyViewController alloc] init];
    scVC.index = 1;
    WS(weakSelf)
    scVC.companyBlock = ^(CompanyModel *company) {
        self.selectCompany = company;
//        self.targetCompanyL.text = company.companyAbbreviatedName;
        [weakSelf updateTargetCompany];
    };
    [self.navigationController pushViewController:scVC animated:YES];
}

- (IBAction)currentStatusBtnClick:(id)sender {
    CurrentStatusPopView *statusV = [[CurrentStatusPopView alloc] initWithStatusArray:self.status andSelectedStatus:self.currentStatusL.text];
    WS(weakSelf)
    statusV.selectBlock = ^(NSString *status) {
        for (NSInteger i = 0; i < weakSelf.status.count; i++) {
            if ([[weakSelf.status objectAtIndex:i] isEqualToString:status]) {
                _stateIndex = i;
                break;
            }
        }
        [weakSelf updateTargetCompany];
    };
    [statusV show];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 95;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ManageJobIntensionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ManageJobIntensionCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.dataArray objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AddJobIntensionVC *add = [[AddJobIntensionVC alloc] init];
    add.index = 1;
    add.model = [self.dataArray objectAtIndex:indexPath.row];
    add.addBlock = ^{
        /** 查询意向职位列表 */
        [self loadIntentionPositionData];
        /** 获取求职者目标公司及状态 */
        [self loadTargetCompany];
    };
    [self.navigationController pushViewController:add animated:YES];
}

- (IBAction)addJobIntensionBtnClick:(id)sender {
    if (self.dataArray.count >= 3) {
        [CommonUtils showToast:@"最多添加三个求职意向"];
        return;
    }
    
    AddJobIntensionVC *add = [[AddJobIntensionVC alloc] init];
    add.index = 0;
    add.addBlock = ^{
        /** 查询意向职位列表 */
        [self loadIntentionPositionData];
        /** 获取求职者目标公司及状态 */
        [self loadTargetCompany];
    };
    [self.navigationController pushViewController:add animated:YES];
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

@end
